#include <ADCTouch.h>
#include <MIDIUSB.h>

int PIN_1 = A0;
int PIN_2 = A1;
int PIN_3 = A2;
int PIN_5 = A3;
int PIN_6 = A4;

bool isPressed_1 = false; // true
bool isPressed_2 = false; // true
bool isPressed_3 = false; // true
bool isPressed_5 = false; // true
bool isPressed_6 = false; // true

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  int value_1 = ADCTouch.read(PIN_1);
  int value_2 = ADCTouch.read(PIN_2);
  int value_3 = ADCTouch.read(PIN_3);
  int value_5 = ADCTouch.read(PIN_5);
  int value_6 = ADCTouch.read(PIN_6);

  // *******
  // DO
  // *******
  if (value_1 > 600 && isPressed_1 == false) {
    Serial.println(value_1);
    noteOn(0, 60, 90);   // Channel 0, middle C, normal velocity
    MidiUSB.flush();
    isPressed_1 = true;
    delay(10);
  }
  if (value_1 < 600) {
    isPressed_1 = false;
    noteOff(0, 60, 90);  // Channel 0, middle C, normal velocity
    MidiUSB.flush();
  }

  // *******
  // RE
  // *******
  if (value_2 > 600 && isPressed_2 == false) {
    Serial.println(value_2);
    noteOn(0, 62, 90);   // Channel 0, middle C, normal velocity
    MidiUSB.flush();
    isPressed_1 = true;
    delay(10);
  }
  if (value_2 < 600) {
    isPressed_2 = false;
    noteOff(0, 62, 90);  // Channel 0, middle C, normal velocity
    MidiUSB.flush();
  }

  // *******
  // MI
  // *******
  if (value_3 > 600 && isPressed_3 == false) {
    Serial.println(value_3);
    noteOn(0, 64, 90);   // Channel 0, middle C, normal velocity
    MidiUSB.flush();
    isPressed_1 = true;
    delay(10);
  }
  if (value_3 < 600) {
    isPressed_3 = false;
    noteOff(0, 64, 90);  // Channel 0, middle C, normal velocity
    MidiUSB.flush();
  }

  // *******
  // SO
  // *******
  if (value_5 > 600 && isPressed_5 == false) {
    Serial.println(value_5);
    noteOn(0, 67, 90);   // Channel 0, middle C, normal velocity
    MidiUSB.flush();
    isPressed_1 = true;
    delay(10);
  }
  if (value_5 < 600) {
    isPressed_5 = false;
    noteOff(0, 67, 90);  // Channel 0, middle C, normal velocity
    MidiUSB.flush();
  }

  // *******
  // LA
  // *******
  if (value_6 > 600 && isPressed_6 == false) {
    Serial.println(value_6);
    noteOn(0, 69, 90);   // Channel 0, middle C, normal velocity
    MidiUSB.flush();
    isPressed_1 = true;
    delay(10);
  }
  if (value_6 < 600) {
    isPressed_6 = false;
    noteOff(0, 69, 90);  // Channel 0, middle C, normal velocity
    MidiUSB.flush();
  }
}